# replication file for JOP

# Justin Melnick
# melnickj@nyu.edu
# last updated: August 19 2024

rm(list = ls())
library(tidyverse)
library(fixest)

# set working directory for the replication file
# setwd('~/PLC_replication/')
data <- read_csv('PLC_replication_data.csv')


# function to create tables
g <- function(dv){
  m <- feols(data = data, fml = as.formula(paste0(dv, ' ~ ', fml,  " | ccode + year")),
             cluster = ~ccode)
}
dict <- c(LW = '$W_{t-1}$', logIncomingTenure = 'Log(Tenure)', LlgdppcWB = 'Log(GDPpc$_{t-1}$)', LlpopWB = 'Log(Population$_{t-1}$)',
          growthWB = 'Growth', ResRents = 'Resource Rents', ccode = 'Country', year = 'Year', 
          wiki_x = 'Leader', leadergrowth = 'Leader Growth')

fml <- 'LW + logIncomingTenure + LlgdppcWB + LlpopWB + growthWB + ResRents'

# note: to create the LaTeX code for the tables, change to tex = T in etable

# table 2 replication
etable(g('total2'), g('Public'), g('ratio'),
       headers = c('Total Rewards', 'Public Goods', '$\\frac{\\text{Private}}{\\text{Private + Public}}$'),
       depvar = F, dict = dict, tex = F, style.tex = style.tex('aer'),
       notes = c('Standard errors clustered by country'),
       digits = 3, digits.stats = 3, order = c('^W', 'Tenure'),
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10)
)

# table 3 replication
etable(g('Assembly'), g('pressRestrict'), g('Torture'), g('PTS_S'),
       headers = c('Assembly Goods', 'Restricting Press', 'Torture', 'PTS'),
       depvar = F, dict = dict, tex = F, style.tex = style.tex('aer'),
       notes = c('Standard errors clustered by country'),
       digits = 3, digits.stats = 3, order = c('^W', 'Tenure'),
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10)
)

# table 4 replication
etable(g('Public'),
       g('HealthExpend'),
       g('EducExpend'),
       g('defense'),
       headers = c('Public Goods', 'Health Spending', 'Educ Spending', 'Defense Spending'),
       depvar = F, dict = dict, tex = F, style.tex = style.tex('aer'),
       notes = c('Standard errors clustered by country'),
       digits = 3, digits.stats = 3, order = c('^W', 'Tenure'),
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10))

# table 5 replication
etable(g('HealthPublic'),
       g('EdPublic'),
       g('DefPublic'),
       headers = c('Health Spending $\\times$ Public', 'Educ Spending $\\times$ Public', 'Defense Spending $\\times$ Public'),
       depvar = F, dict = dict, tex = F, style.tex = style.tex('aer'),
       notes = c('Standard errors clustered by country'),
       digits = 3, digits.stats = 3, order = c('^W', 'Tenure'),
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10))

# table 6 replication
fml <- 'LW + logIncomingTenure + LlgdppcWB + LlpopWB + leadergrowth + ResRents'
etable(g('total2'), g('Public'), g('ratio'),
       headers = c('Total Rewards', 'Public Goods', '$\\frac{\\text{Private}}{\\text{Private + Public}}$'),
       depvar = F, dict = dict, tex = F, style.tex = style.tex('aer'),
       notes = c('Standard errors clustered by country'),
       digits = 3, digits.stats = 3, order = c('^W', 'Tenure'),
       signif.code = c("***"=0.01, "**"=0.05, "*"=0.10)
)
